package com.juzipi.demo.controller;



import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Teacher;
import com.juzipi.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class TeacherController {


    @Autowired
    private TeacherService teacherService;


    @GetMapping("{id}")
    public ResponseEntity<Teacher> queryTeacher(@PathVariable Integer id){
        Teacher teacher = teacherService.queryTeacher(id);
        if (StringUtils.isEmpty(teacher)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(teacher);

    }


    @PostMapping("insert")
    public ResponseEntity<String> addTeacher(@RequestBody Teacher teacher){
        String message = teacherService.addTeacher(teacher);
        if (StringUtils.isEmpty(message)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(message);
    }


    @PostMapping("update")
    public ResponseEntity<String> updateTeacher(@RequestBody Teacher teacher){
        String message = teacherService.updateTeacher(teacher);
        if (StringUtils.isEmpty(message)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(message);
    }

    @PostMapping("{id}")
    public ResponseEntity<String> deleteTeacherById(@PathVariable Integer id){
        String message = teacherService.deleteTeacherById(id);
        if (StringUtils.isEmpty(message)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(message);
    }


    //查询老师的信息和他所教的课程（多对多查询）
    //也算是实现了，返回了老师的信息加 subject 类的信息
    @PostMapping("tesub")
    public ResponseEntity<PageResult<Teacher>> queryTeacherSubject(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize
    ){
        PageResult<Teacher> pageResult = teacherService.queryTeacherSubject(key,pageNum,pageSize);
        if (CollectionUtils.isEmpty(pageResult.getItems())){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 意图是根据老师姓名查询老师所对应的 学生的信息
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("tecstu")
    public ResponseEntity<Map<String,Object>> queryTeacherStudent(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize
    ){
        Map<String,Object> teachersStudent = teacherService.queryTeacherStudent(key,pageNum,pageSize);
        if (CollectionUtils.isEmpty(teachersStudent)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.OK).body(teachersStudent);

    }

}
